package com.corner.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.corner.R;
import com.corner.app.ConsTants;
import com.corner.model.db.DBConstans;
import com.corner.model.db.Session;
import com.corner.presenter.imp.SessionItemPresenter;
import com.corner.ui.activity.ChatActivity;
import com.corner.ui.activity.ProfileActivity;
import com.corner.ui.base.BaseHolder;
import com.corner.ui.view.ISessionItemView;
import com.corner.utils.DateFormatUtil;
import com.corner.utils.view.CustomFont;
import com.corner.utils.view.FindView;
import com.corner.utils.view.InjectViewUtils;
import com.daimajia.swipe.SwipeLayout;
import com.daimajia.swipe.adapters.BaseSwipeAdapter;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.List;

/**
 * Created by WeiHey on 2015/5/15.
 */
public class SessionAdapter extends BaseSwipeAdapter implements View.OnClickListener {
    private List<Session> sessions;
    private Context context;


    public SessionAdapter(List<Session> sessions, Context context) {
        this.sessions = sessions;
        this.context = context;
    }

    @Override
    public int getCount() {
        return sessions.size();
    }

    @Override
    public Object getItem(int position) {
        return sessions.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getSwipeLayoutResourceId(int i) {
        return R.id.swipe_frag_session;
    }

    @Override
    public View generateView(int position, ViewGroup viewGroup) {
        View convertView = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.lv_sessionitem, null);
            Holder holder = new Holder();
            InjectViewUtils.injectView(holder, convertView, context);
            convertView.setTag(holder);

            SwipeLayout swipeLayout = holder.swipeLayout;
            swipeLayout.setShowMode(SwipeLayout.ShowMode.PullOut);
            swipeLayout.addDrag(SwipeLayout.DragEdge.Right, holder.tvDelete);
            swipeLayout.setClickToClose(true);
            swipeLayout.setDragDistance(300);

        }
        return convertView;
    }

    @Override
    public void fillValues(int position, View view) {
        final Holder holder = (Holder) view.getTag();
        holder.ivAvater.setImageURI(null);
        holder.tvNickName.setText(null);
        final Session session = sessions.get(position);
        holder.tvNickName.setTag(session);
        holder.requestUser(session.getSessionName());
        if (session.getType()!=null) {
            switch (session.getType()) {
                case DBConstans.TEXT:
                    holder.tvContent.setText(session.getText());
                    break;
                case DBConstans.COMMENT:
                    holder.tvContent.setText(context.getString(R.string.session_comment_notice) + session.getText());
                    break;
                case DBConstans.SHARE:
                    holder.tvContent.setText(context.getString(R.string.session_share_notice) + session.getText());
                    break;
                case DBConstans.AUDIO:
                    holder.tvContent.setText(context.getString(R.string.session_audio_notice));
                    break;
                case DBConstans.PIC:
                    holder.tvContent.setText(context.getString(R.string.session_pic_notice));
                    break;
                case DBConstans.PIC_AUDIO:
                    holder.tvContent.setText(context.getString(R.string.session_pic_audio_notice));
                    break;
                case DBConstans.LIKE:
                    holder.tvContent.setText(context.getString(R.string.session_like));
                    break;
            }
        }
        if (!session.getIsReaded()) {
            holder.ivReaded.setVisibility(View.VISIBLE);
        } else {
            holder.ivReaded.setVisibility(View.INVISIBLE);
        }
        holder.tvTime.setText(DateFormatUtil.getShowTimeWithHmInTodayOrMdBeforeToday(session.getTimestamp().getTime(), context));
        holder.llContent.setOnClickListener(this);
        holder.ivAvater.setTag(session);
        holder.ivAvater.setOnClickListener(this);
        holder.llContent.setTag(session);
        holder.tvDelete.setOnClickListener(this);
        holder.tvDelete.setTag(holder);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_frag_session_avater:
                Intent intent = new Intent(context, ProfileActivity.class);
                Session session = (Session) v.getTag();
                intent.putExtra(ProfileActivity.USER_ID, Long.valueOf(session.getSessionName()));
                context.startActivity(intent);
                break;
            case R.id.ll_frag_session_content:
                intent = new Intent(context, ChatActivity.class);
                intent.putExtra(ChatActivity.INTENT_ID, ChatActivity.CHAT);
                session = (Session) v.getTag();
                intent.putExtra(ChatActivity.USER_ID_IN_LONG,Long.valueOf(session.getSessionName()));
                context.startActivity(intent);
                break;
            case R.id.tv_lv_frag_session_delete:
                Holder holder = (Holder) v.getTag();
                holder.delete();
                break;
        }
    }

    public void changeList(List<Session> sessions) {
        this.sessions = sessions;
        notifyDataSetChanged();
    }

    class Holder extends BaseHolder<SessionItemPresenter> implements ISessionItemView {
        @FindView(R.id.iv_frag_session_avater)
        public SimpleDraweeView ivAvater;
        @FindView(R.id.ll_frag_session_content)
        public LinearLayout llContent;
        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_frag_session_nickname)
        public TextView tvNickName;
        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_frag_session_time)
        public TextView tvTime;
        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_frag_session_content)
        public TextView tvContent;
        @CustomFont(ConsTants.TYPE_FACE)
        @FindView(R.id.tv_frag_session_divider)
        public TextView tvDivider;
        @FindView(R.id.iv_frag_session_readed)
        public ImageView ivReaded;
        @FindView(R.id.tv_lv_frag_session_delete)
        public TextView tvDelete;
        @FindView(R.id.swipe_frag_session)
        public SwipeLayout swipeLayout;
        private String userId;

        @Override
        public void setName(String userId, String name) {
            if (userId.equals(this.userId))
                tvNickName.setText(name);
        }

        @Override
        public void setAvaterUri(String userId, Uri uri) {
            if (userId.equals(this.userId))
                ivAvater.setImageURI(uri);
        }

        @Override
        protected SessionItemPresenter createP() {
            return new SessionItemPresenter();
        }

        @Override
        public void showLoading(boolean isShow) {
        }

        public void requestUser(String userId) {
            this.userId = userId;
            presenter.getUser(userId);
        }

        public void delete() {
            presenter.deleteUser(userId);
        }
    }
}
